package me.zhengjie.modules.finace_taxation.rest;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.base.PageInfo;
import me.zhengjie.modules.finace_taxation.domain.FtService;
import me.zhengjie.modules.finace_taxation.service.FtServiceService;
import me.zhengjie.modules.finace_taxation.service.dto.*;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "service管理")
@RequestMapping("/api/ft/ftService")
public class FtServiceController {

    private final FtServiceService ftServiceService;

    @GetMapping
    @Log("查询服务列表")
    @ApiOperation("查询服务列表")
//    @PreAuthorize("@el.check('ftService:list')")
    public ResponseEntity query(@RequestParam Map<String,Object> map, Pageable pageable){
        return new ResponseEntity<>(ftServiceService.queryAll(map,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增服务")
    @ApiOperation("新增服务")
//    @PreAuthorize("@el.check('ftService:add')")
    public ResponseEntity create(@Validated @RequestBody FtServiceDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftServiceService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改服务")
    @ApiOperation("修改服务")
//    @PreAuthorize("@el.check('ftService:edit')")
    public ResponseEntity update(@Validated @RequestBody FtServiceDto resources){
        ftServiceService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除服务")
    @ApiOperation("删除服务")
//    @PreAuthorize("@el.check('ftService:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftServiceService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/putShelf")
    @Log("服务上下架")
    @ApiOperation("服务上下架")
    public ResponseEntity updateById(@RequestBody FtService ftService) {
        JSONObject jsonObject = new JSONObject();
        try {
            ftServiceService.putShelfService(ftService);
            jsonObject.put("success",true);
            jsonObject.put("message","操作成功");
            jsonObject.put("code",200);
            return new ResponseEntity<>(jsonObject,HttpStatus.OK);
        }catch (Exception e){
            jsonObject.put("success",false);
            jsonObject.put("message","操作失败");
            jsonObject.put("code",500);
            return new ResponseEntity<>(jsonObject,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/popularService")
    @Log("热门服务推送")
    @ApiOperation("热门服务推送")
    public ResponseEntity popularService(@RequestBody FtService ftService) {
        JSONObject jsonObject = new JSONObject();
        try {
            ftServiceService.popularService(ftService);
            jsonObject.put("success",true);
            jsonObject.put("message","操作成功");
            jsonObject.put("code",200);
            return new ResponseEntity<>(jsonObject,HttpStatus.OK);
        }catch (Exception e){
            jsonObject.put("success",false);
            jsonObject.put("message","操作失败");
            jsonObject.put("code",500);
            return new ResponseEntity<>(jsonObject,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/getPutShelfServicesByType")
    @Log("小程序查询服务")
    @ApiOperation("小程序查询服务")
//    @PreAuthorize("@el.check('ftService:list')")
    public ResponseEntity getPutShelfServicesByType(@RequestBody Map<String,Object> map){
        return new ResponseEntity<>(ftServiceService.getPutShelfServicesByType(map),HttpStatus.OK);
    }


    @PostMapping("/serviceFavorite")
    @Log("服务收藏")
    @ApiOperation("服务收藏")
//    @PreAuthorize("@el.check('ftService:list')")
    public ResponseEntity serviceFavorite(@RequestBody FtServiceFacoriteDto ftServiceFacoriteDto){
        JSONObject jsonObject = new JSONObject();
        try {
            ftServiceService.serviceFavorite(ftServiceFacoriteDto);
            jsonObject.put("success",true);
            jsonObject.put("message","操作成功");
            jsonObject.put("code",200);
            return new ResponseEntity<>(jsonObject,HttpStatus.OK);
        }catch (Exception e){
            jsonObject.put("success",true);
            jsonObject.put("message","操作失败");
            jsonObject.put("code",500);
            return new ResponseEntity<>(jsonObject,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/getServiceProcess/{serviceId}")
    @Log("查询服务的流程")
    @ApiOperation("查询服务的流程")
    public ResponseEntity query(@PathVariable("serviceId") String serviceId){
        return new ResponseEntity<>(ftServiceService.getServiceProcess(serviceId),HttpStatus.OK);
    }

    @GetMapping("/addServiceBrowseNumber/{serviceId}")
    @Log("修改服务浏览量")
    @ApiOperation("修改服务浏览量")
    public ResponseEntity addServiceBrowseNumber(@PathVariable("serviceId") String serviceId){
        return new ResponseEntity<>(ftServiceService.addServiceBrowseNumber(serviceId),HttpStatus.OK);
    }

    @GetMapping("/addServiceAdvisoryNumber/{serviceId}")
    @Log("修改服务咨询量")
    @ApiOperation("修改服务咨询量")
    public ResponseEntity addServiceAdvisoryNumber(@PathVariable("serviceId") String serviceId){
        return new ResponseEntity<>(ftServiceService.addServiceAdvisoryNumber(serviceId),HttpStatus.OK);
    }
    // 服务收藏列表
    @PostMapping("/queryServiceByUserId")
    @Log("根据用户id查询服务收藏表")
    @ApiOperation("根据用户id查询服务收藏表")
    public JSONObject queryServiceByUserId(@RequestBody Map<String, Object> map){
        JSONObject resObj = new JSONObject();
        PageInfo<FtService> pageInfo = ftServiceService.queryServiceByUserId(map);
        resObj.put("data", pageInfo);
        resObj.put("success", true);
        resObj.put("message", "查询成功");
        resObj.put("code", 200);
        return resObj;
    }

    @PostMapping("/cacelServiceFavorite")
    @Log("取消服务收藏")
    @ApiOperation("取消服务收藏")
    public ResponseEntity cacelServiceFavorite(@RequestBody FtServiceFacoriteDto ftServiceFacoriteDto){
        return new ResponseEntity<>(ftServiceService.cacelServiceFavorite(ftServiceFacoriteDto.getUserId(),ftServiceFacoriteDto.getServiceId()),HttpStatus.OK);
    }

    @PostMapping("/getServiceFavoriteCount")
    @Log("查询是否收藏过该服务")
    @ApiOperation("查询是否收藏过该服务")
    public ResponseEntity getServiceFavoriteCount(@RequestBody FtServiceFacoriteDto ftServiceFacoriteDto){
        return new ResponseEntity<>(ftServiceService.getServiceFavoriteCount(ftServiceFacoriteDto.getUserId(),ftServiceFacoriteDto.getServiceId()),HttpStatus.OK);
    }

    // 根据类型查询服务
    @PostMapping("/searchByType")
    @Log("根据类型查询服务")
    @ApiOperation("根据类型查询服务")
    public JSONObject searchByType(@RequestBody Map<String, Object> map){
        JSONObject resObj = new JSONObject();
        List<FtService> list = ftServiceService.searchByType(map);
        resObj.put("data", list);
        resObj.put("success", true);
        resObj.put("message", "查询成功");
        resObj.put("code", 200);
        return resObj;
    }

    // 根据服务id查询服务的剩余活动时间
    @GetMapping("/getLimitDateByServiceId/serviceId/{serviceId}")
    @Log("根据服务id查询服务的剩余活动时间")
    @ApiOperation("根据服务id查询服务的剩余活动时间")
    public FtLimitDateDto getLimitDateByServiceId(@PathVariable("serviceId") String serviceId){
        return ftServiceService.getLimitDateByServiceId(serviceId);
    }

    @Log("导出服务")
    @ApiOperation("导出服务")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtServiceQueryParam query) throws IOException {
        ftServiceService.download(ftServiceService.queryAll(query), response);
    }

}
